<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      :show-message="false"
      class="login-form"
      autocomplete="on"
      label-position="left"
      :validate-on-rule-change="false"
    >
      <div class="login-logo">
        <div class="logo">
          <el-avatar :src="avatarUrl.url" style="width: 100%; height: 100%" />
        </div>
      </div>
      <div class="title-container">
        <h3 class="title">本无名片-后台管理系统</h3>
      </div>
      <!-- 用户名输入框 -->
      <el-form-item prop="username">
        <!-- 用户图标 -->
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入账号/工号/手机号"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password" style="margin-bottom: 0;">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入登录密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <!-- ？显示密码 -->
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-tooltip>
      <!-- <div class="passLabel">默认密码: 123456</div> -->
      <el-button
        :loading="loading"
        type="primary"
        @click.native.prevent="handleLogin"
      >
        登 录
      </el-button>

      <div class="login-btns" style="display: none">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>测试账号</span>
          </div>
          <template v-for="(user, index) in users">
            <div :key="user.user + '1'" class="span1" @click="changForm(user)">{{ user.name }}</div>
            <el-divider v-if="index + 1 < users.length" :key="user.user + '2'" style="margin: 10px 0" />
          </template>
        </el-card>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateForm = (rule, value, callback) => {
      if (value.trim().length > 0) {
        callback()
      } else {
        callback(
          new Error(rule.field === 'username' ? '请输入账号' : '请输入密码')
        )
      }
    }
    return {
      users: [
        { name: '王林', user: 'fxi', pass: '123456' },
        { name: '超级管理员', user: 'admin', pass: '123456' },
        { name: '冯健', user: 'fj', pass: '123456' },
        { name: '尹鑫', user: 'yx', pass: '123456' }
      ],
      avatarUrl: { url: require('@/assets/img/logo.png') },
      loginForm: {
        username: 'chenjiao',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateForm }],
        password: [{ required: true, trigger: 'blur', validator: validateForm }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    '$route': {
      handler(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    changForm(user) {
      this.loginForm.username = user.user
      this.loginForm.password = user.pass
      this.handleLogin()
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          // 登录
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.$store.dispatch('user/getCurrentUserInfo')
              // 菜单权限
              // this.$store.dispatch('user/menu').then(res => {
              //   window.location.reload(true)
              //   if (res.code === 0) {
              //     this.$router.push({
              //       // path: this.redirect || '/',
              //       path: '/',
              //       query: this.otherQuery
              //     })
              //     this.loading = false
              //   }
              // })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          this.$message.error('请输入正确的账号或者密码')
          this.loading = false
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">

.passLabel{
    font-size: 14px;
    color: #b3b3b3;
    margin: 5px 0 20px;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #e3e4e6;
// 输入字体颜色
$light_gray: rgb(22, 22, 22);
// 光标颜色
$cursor: rgb(22, 22, 22);

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      // 修复input 背景不协调
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border-bottom: 1px solid rgba(22, 22, 22, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    color: #454545;
    margin-bottom: 20px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background-size: 100% 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    background: #fff;
    position: relative;
    width: 350px;
    border-radius: 8px;
    max-width: 100%;
    padding: 40px 30px;
    // overflow: hidden;
    border: 1px solid #ffffff;
    .login-btns{
      width: 200px;
      position: absolute;
      left: 360px;
      top: 0;
      .span1{
        color: #409eff;
        cursor: pointer;
        &:active{
          opacity: 0.7;
        }
      }
      .el-divider{
        margin: 20px 0;
      }
    }
  }

  // .tips {
  //   font-size: 14px;
  //   color: #fff;
  //   margin-bottom: 10px;

  //   span {
  //     &:first-of-type {
  //       margin-right: 16px;
  //     }
  //   }
  // }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  // logo图标
  .login-logo {
    // position: relative;
    // background: green;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    margin-top: 20px;

    .logo {
      width: 72px;
      height: 72px;
    }
  }

  .title-container {
    position: relative;
    .title {
      font-size: 24px;
      color: #1c92ff;
      margin: 0px 0px 40px 0px;
      text-align: center;
      font-weight: bold;
    }
  }
  // 查看密码
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  // .thirdparty-button {
  //   position: absolute;
  //   right: 0;
  //   bottom: 6px;
  // }

  // @media only screen and (max-width: 470px) {
  //   .thirdparty-button {
  //     display: none;
  //   }
  // }
  // 登录按钮
  .el-button {
    width: 100%;
    height: 40px;
    font-size: 20px;
    font-weight: 500;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 20px;
    margin: 50px 0px 30px 0px;
    box-shadow: 0px 0px 20px 0px rgba(28, 146, 255, 0.3);
  }
}
</style>

